205D - Little Elephant and Cards - CodeForces Solution


binary search brute force sortings *1500

Please click on ads to support us..

C++ Code:

/*
ID: laithal2
TASK: beads
PROG: beads
LANG: C++
*/

#include <bits/stdc++.h>
using namespace std;
#define ll      long long
#define ull     unsigned long long
#define _for(i, n)      for(ll i = 0; i < n; i++)
#define _forv(i, a, n)      for(ll i = a; i < n; i++)
#define _forvIncr(i, a, n, k)      for(ll i = a; i < n; i+=k)
#define _forvRev(i, a, n)      for(ll i = a; i >= n; i--)
#define _for1(i, n)         for(ll i = 1; i <= n; i++)
#define vbe(vec)    vec.begin(), vec.end()
#define uniqueVec(vec) vec.resize(distance(vec.begin(), unique(vbe(vec))));
#define mp(f,s)     make_pair(f, s)
#define pb(e) push_back(e)
#define cond_cout(cond) if(cond) puts("YES"); else puts("NO");
//#define endl '\n'
const int MOD = 1e9 + 7;
const int maxN = 1e6 + 1;

inline void f()
{
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
}


map<ll, ll> mpf, mpb;
int main()
{
    f();
    ll n;
    cin >> n;
    _for(i, n){
        int x, y;
        cin >> x >> y;
        if(x == y){
            mpf[x]++;
        }
        else{
            mpf[x]++;
            mpb[y]++;
        }
    }
    ll ans = 1e9;
    int h = ceil(n/2.0);
    for(auto p : mpf){
        if(p.second + mpb[p.first] >= h){
            ans = min(ans, max(0ll, h - p.second));
        }
    }
    if(ans == 1e9){
        for(auto p : mpb){
            if(p.second >= h) ans = h;
        }
    }
    if(ans == 1e9) ans = -1;
    cout << ans << endl;
    return 0;
}
/*

*/


Comments

Submit
0 Comments
More Questions

1670D - Very Suspicious
1141B - Maximal Continuous Rest
1341A - Nastya and Rice
1133A - Middle of the Contest
385A - Bear and Raspberry
1311B - WeirdSort
1713F - Lost Array
236B - Easy Number Challenge
275A - Lights Out
147A - Punctuation
253A - Boys and Girls
1327E - Count The Blocks
984A - Game
12B - Correct Solution
1355B - Young Explorers
485A - Factory
628A - Tennis Tournament
1436B - Prime Square
1707B - Difference Array
1422C - Bargain
1611F - ATM and Students
660A - Co-prime Array
1692F - 3SUM
1470A - Strange Birthday Party
190D - Non-Secret Cypher
1721B - Deadly Laser
1721C - Min-Max Array Transformation
1721A - Image
1180C - Valeriy and Deque
557A - Ilya and Diplomas